package com.future.bantang.activity;

import android.view.ViewTreeObserver;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.future.bantang.R;
import com.future.bantang.eventmas.BaseEventMsg;
import com.future.bantang.fragment.BaseFragment;
import com.future.bantang.fragment.HomeFragment;
import com.future.bantang.fragment.SquareFragment;
import com.future.bantang.utils.L;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnRadioGroupCheckedChange;

import de.greenrobot.event.EventBus;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {
    @ViewInject(R.id.rg_main_tab)
    private RadioGroup mRgHomeTabGroup;
    @ViewInject(R.id.layout_fragment_containor)
    private RelativeLayout mFragmentContainor;


    public class HeightMsg extends BaseEventMsg<Void>{
    }

    private boolean isMeasure=false;
    @Override
    protected void init() {
        super.init();

        ViewTreeObserver viewTreeObserver=mFragmentContainor.getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (!isMeasure) {
                    //测量fragmentContainor的高度，用于homeFragment中设置body的高度
                    int fragmentContainorHeight = mFragmentContainor.getMeasuredHeight();
                    L.d("tabGroupHeight=" + fragmentContainorHeight);
                    HeightMsg heightMsg = new HeightMsg();
                    heightMsg.setArg1(fragmentContainorHeight);
                    EventBus.getDefault().postSticky(heightMsg);
                    isMeasure=true;
                }
                return true;
            }
        });

    }



    @Override
    protected void loadDatas() {
        super.loadDatas();
        RadioGroup radioGroup= (RadioGroup) findViewById(R.id.rg_main_tab);
        RadioButton radioButton= (RadioButton) radioGroup.getChildAt(0);
        radioButton.performClick();

    }

    @OnRadioGroupCheckedChange(R.id.rg_main_tab)
    private void curTabChange(RadioGroup group, int checkedId){
        Class<? extends BaseFragment> cls=null;
        switch (checkedId){
            case R.id.rb_main_tab_home:
                cls= HomeFragment.class;
                break;
            case R.id.rb_main_tab_community:
                //work by liufujie
                cls = SquareFragment.class;
                break;
        }
        if (cls!=null){
            showFragment(R.id.layout_fragment_containor,cls);
        }
    }





    private boolean isFirst=false;
    private long lastTime=0;
    @Override
    public void onBackPressed() {
        if (!isFirst){
            isFirst=true;
            Toast.makeText(MainActivity.this, R.string.one_more_out, Toast.LENGTH_SHORT).show();
            lastTime=System.currentTimeMillis();
        }else {
            long curTime=System.currentTimeMillis();
            if (curTime-lastTime>2000){
                Toast.makeText(MainActivity.this, R.string.one_more_out, Toast.LENGTH_SHORT).show();
                lastTime=System.currentTimeMillis();
            }else {
                finish();
            }

        }
    }
}
